Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Why use { with generate (probably trivial)?

    Hello
    A colleague (who I can no longer contact) left me a do-file with code like this:

    Code:
     {
    capture drop fulltime
    gen fulltime = 0 if  X==2 
    replace fulltime = 1 if  X==1 
    lab def fulltime 0 "Part time " 1 "Full time", replace
    lab val fulltime fulltime
    }
    These lines of code are used several times in the same do-file and I don't know why it is just not used once. Am I missing something?

    Thanks
    Laurence

  • #2
    The lines are a block of code that calculates a variable fulltime from the value of variable X. There is nothing about this code, per se, that requires wrapping it in curly braces. Nor is there anything about this code, per se, that would cause it to appear multiple times.

    In other words, the answers to your questions lie not in the code itself but in the context of the rest of the code in the do-file, which you have not shown. The possible circumstances that would warrant this repetition of the code are numerous, so it would be foolish of me to speculate, and impractical to attempt to enumerate them all.

    Comment


    • #3
      Thanks Clyde Schechter Your answer covers my question.
      Your point about the rest of the file is, of course, reasonable, but the very long do-file just produces multiple Figures - nothing more complicated.
      Thanks
      Laurence

      Comment


      • #4
        As a matter of personal style preference, I would consider this bad coding practice since it is adding superfluous punctuation that do nothing, in and of themselves. If signalling a block of code to generate a variable, having no line breaks between the lines does that, or even better, a comment before the block of code explaining what it does (or why).

        Comment

        Working...
        X